﻿@using OA.AspNetCore.Html;
@using OA.Model;
@using OA.Core.Model;
@using System.Text
@using OA.Core.Helper
@model List<Dictionary<string, string>>

@{
    Layout = null;
    string tableAttribute = ViewData["TableAttribute"].ToString();
    string tableName = ViewData["TableName"].ToString();
    List<ExtendField> list = OA.Core.Helper.JsonHelper.DeserializeList<ExtendField>(ViewData["TableInfo"].ToString());


    Queue<string> parentsName = new Queue<string>();
    int index = 0;
    Dictionary<string, int> dicIndex = new Dictionary<string, int>();
    Dictionary<string, int> dicColspan = new Dictionary<string, int>();
    foreach (var item in list)
    {
        if (!parentsName.Contains(item.ParentName))
        {
            parentsName.Enqueue(item.ParentName);
            item.Sort = index;
            dicIndex.Add(item.ParentName, index);
            dicColspan.Add(item.ParentName, 1);
        }
        else
        {
            item.Sort = dicIndex[item.ParentName];
            dicColspan[item.ParentName] += 1;
        }
    }
    list = list.OrderBy(m => m.Sort).ToList();

    string jsContent = string.Empty;
    bool hasStatistics = false;
    bool hasSummarizing = false;
    Dictionary<string, decimal> dicSummarizing = new Dictionary<string, decimal>();
    decimal total = 0;
    int colspan = list.Count + 1;

    string trClass = "";
    if (parentsName.Count != list.Count)
    {
        trClass = "children";
    }
}


<table class="layui-table extendTable" style="margin-top: 10px; border-collapse: unset; text-align: center;">
    <thead>
        <tr><th colspan="@colspan" style="text-align:center;font-weight:bold;">@tableName 详情</th></tr>
        @if (list != null)
        {
            if (parentsName.Count != list.Count)
            {
                <tr>
                    <td rowspan="2">#</td>
                    @foreach (var parentName in parentsName)
                    {
                        int children = dicColspan[parentName];
                        if (children == 1)
                        {
                            <td rowspan="2" style="text-align:center;">@parentName</td>
                        }
                        else
                        {
                            <td colspan="@children" style="text-align:center;">@parentName</td>
                        }
                    }
                </tr>
            }
        }
    <tr class="@trClass">
        @if (parentsName.Count == list.Count)
        {
            <td>#</td>
        }
        @foreach (var item in list)
        {
            if (parentsName.Count == list.Count || dicColspan[item.ParentName] > 1)
            {
                if (item.Type == 69)
                {

                    <th style="text-align:right;padding-right:12px;">@item.Name</th>
                }
                else
                {
                    <th style="text-align:center;">@item.Name</th>
                }
            }             
        }
    </tr>
    </thead>
    <tbody>
        @for (int i = 0; i < Model.Count; i++)
        {
            var valueImte = Model[i];
            int tempIndex = i + 1;
        <tr>
            <td>@tempIndex</td>
            @for (int j = 0; j < list.Count; j++)
            {
                ExtendField infoExtendField = new ExtendField();
                infoExtendField = list[j];

                if (valueImte.ContainsKey(infoExtendField.Attribute))
                {
                    infoExtendField.Value = valueImte[infoExtendField.Attribute];
                    if (infoExtendField.Type == 69)
                    {
                        hasStatistics = true;
                        decimal tempValue = 0;
                        if (decimal.TryParse(infoExtendField.Value, out tempValue))
                        {
                            total += tempValue;
                        }

                    }
                    if ((infoExtendField.Type == 21 && infoExtendField.IsSummarizing) || infoExtendField.Type == 69)
                    {
                        hasSummarizing = true;
                        decimal tempValue = 0;
                        if (decimal.TryParse(infoExtendField.Value, out tempValue))
                        {
                            if (dicSummarizing.ContainsKey(infoExtendField.Attribute))
                            {
                                dicSummarizing[infoExtendField.Attribute] += tempValue;
                            }
                            else
                            {
                                dicSummarizing.Add(infoExtendField.Attribute, tempValue);
                            }
                        }

                    }
                }
                if (infoExtendField.Type == 69)
                {
                    <td style="text-align:right;padding-right:12px;">
                        @infoExtendField.Value.NumberToString(2)
                    </td>
                }
                else if (infoExtendField.Type == 7)
                {
                    if (infoExtendField.Value.ToLower() == "true")
                    {
                        infoExtendField.Value = "是";
                    }
                    else
                    {
                        infoExtendField.Value = "否";
                    }
                    <td style="text-align:center;">
                        @infoExtendField.Value
                    </td>
                }               
                else
                {
                    <td style="text-align:center;">
                        @infoExtendField.Value
                    </td>
                }
            }
        </tr>
        }
    </tbody>
    @if (hasStatistics || hasSummarizing)
    {
        int tempColspan = colspan - 1;
        <tfoot>
            @if (hasSummarizing)
            {
                int tempColspan_1 = 1;
                if(!dicSummarizing.ContainsKey(list[0].Attribute))
                {
                    tempColspan_1 = 2;
                }
                <tr>
                    <td class="layui-table-header" style="font-size:12px;" colspan="@tempColspan_1">汇总</td> @*如果第一列不是汇总列，汇总占两个单元格*@
                    @for (int j = 0; j < list.Count; j++)
                    {
                        decimal summarizingValue = 0;
                        if (dicSummarizing.ContainsKey(list[j].Attribute))
                        {
                            summarizingValue = dicSummarizing[list[j].Attribute];
                            if (list[j].Type == 69)
                            {
                                <td style="text-align: center; background-color: #f2f2f2;text-align:right;padding-right:12px;">@summarizingValue.ToString("0.00")</td>
                            }
                            else
                            {
                                <td style="text-align: center; background-color: #f2f2f2">@summarizingValue.ToString("0.##")</td>
                            }

                        }
                        else if(j!=0)
                        {
                            <td></td>
                        }
                    }
                </tr>
            }
            @if (hasStatistics)
            {
                <tr><td class="layui-table-header" colspan="2">大写</td><td colspan="@tempColspan" style="text-align:right;padding-right:12px;font-size:14px;">￥：@total.CmycurD()</td></tr>
            }
        </tfoot>
    }
</table>
